package hr.fer.ztel.rassus.brbljaonica.gui;

import hr.fer.ztel.rassus.brbljaonica.business.BusinessProvider;
import hr.fer.ztel.rassus.brbljaonica.business.User;

import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.CardLayout;
import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import org.jivesoftware.smack.XMPPException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

/**
 * Klasa koja predstavlja panel za ispunjavanje registracije<br>
 * novog korisnika
 * 
 */
public class RegistrationFormPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField usernameText;
	private JPasswordField passwordField;
	private JPasswordField confirmPasswordField;
	private JLabel lblUsernameSpec;
	private JLabel lblUsernameAlreadyExists;
	private JLabel label;
	private JLabel lblPasswordsDontMatch;
	private JTextField serverTextField;
	private JLabel lblServerNotSpecified;
	private JLabel lblServerNotFound;
	public RegistrationFormPanel() {
		setLayout(null);
		
		JLabel lblRegistrationForm = new JLabel("Registration form");
		lblRegistrationForm.setFont(new Font("Tahoma", Font.PLAIN, 25));
		lblRegistrationForm.setBounds(10, 11, 192, 31);
		add(lblRegistrationForm);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 73, 48, 14);
		add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(11, 104, 46, 14);
		add(lblPassword);
		
		JLabel lblConfirmPassword = new JLabel("Confirm password");
		lblConfirmPassword.setBounds(10, 135, 86, 14);
		add(lblConfirmPassword);
		
		usernameText = new JTextField();
		usernameText.setBounds(113, 70, 144, 20);
		add(usernameText);
		usernameText.setColumns(10);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(113, 101, 144, 20);
		add(passwordField);
		
		confirmPasswordField = new JPasswordField();
		confirmPasswordField.setBounds(113, 132, 144, 20);
		add(confirmPasswordField);
		
		JButton btnSubmit = new JButton("Submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean error = false;
				
				lblServerNotFound.setVisible(false);
				
				if (usernameText.getText().isEmpty()) {
					lblUsernameSpec.setVisible(true);
					error = true;
				} else {
					lblUsernameSpec.setVisible(false);
				}
				
				if (!String.valueOf(passwordField.getPassword()).equals(String.valueOf(confirmPasswordField.getPassword()))) {
					label.setVisible(true);
					lblPasswordsDontMatch.setVisible(true);
					error = true;
				} else {
					label.setVisible(false);
					lblPasswordsDontMatch.setVisible(false);
				}
				
				if (serverTextField.getText().isEmpty()) { 
					error = true;
					lblServerNotSpecified.setVisible(true);
				} else {
					lblServerNotSpecified.setVisible(false);
				}
				
				if (error) return;
				
				User user = new User(usernameText.getText(), "", "", new String(passwordField.getPassword()));
				
				try {
					BusinessProvider.registerNewUser(user, serverTextField.getText().trim() );
				} catch (XMPPException e1) {
					if (e1.getMessage().contains("409")) {
						lblUsernameSpec.setVisible(false);
						lblUsernameAlreadyExists.setVisible(true);
						return;
					} else {
						lblServerNotFound.setVisible(true);
						return;
					}
				}
				
				lblUsernameAlreadyExists.setVisible(false);
				((CardLayout)RegistrationFormPanel.this.getParent().getLayout()).show(RegistrationFormPanel.this.getParent(), "login");
			}
		});
		btnSubmit.setBounds(113, 244, 89, 23);
		add(btnSubmit);
		
		lblUsernameSpec = new JLabel("Username not specified");
		lblUsernameSpec.setForeground(Color.RED);
		lblUsernameSpec.setBounds(267, 73, 133, 14);
		lblUsernameSpec.setVisible(false);
		add(lblUsernameSpec);
		
		lblUsernameAlreadyExists = new JLabel("Username already exists");
		lblUsernameAlreadyExists.setForeground(Color.RED);
		lblUsernameAlreadyExists.setBounds(267, 73, 117, 14);
		lblUsernameAlreadyExists.setVisible(false);
		add(lblUsernameAlreadyExists);
		
		label = new JLabel("}");
		label.setFont(new Font("Tahoma", Font.PLAIN, 31));
		label.setBounds(267, 95, 15, 45);
		label.setVisible(false);
		add(label);
		
		lblPasswordsDontMatch = new JLabel("Passwords do not match");
		lblPasswordsDontMatch.setForeground(Color.RED);
		lblPasswordsDontMatch.setBounds(290, 114, 133, 14);
		lblPasswordsDontMatch.setVisible(false);
		add(lblPasswordsDontMatch);
		
		serverTextField = new JTextField();
		serverTextField.setBounds(113, 161, 144, 20);
		add(serverTextField);
		serverTextField.setColumns(10);
		
		JLabel lblServer = new JLabel("Server");
		lblServer.setBounds(10, 164, 46, 14);
		add(lblServer);
		
		lblServerNotSpecified = new JLabel("Server not specified");
		lblServerNotSpecified.setForeground(Color.RED);
		lblServerNotSpecified.setBounds(267, 164, 133, 14);
		lblServerNotSpecified.setVisible(false);
		add(lblServerNotSpecified);
		
		lblServerNotFound = new JLabel("Server not found");
		lblServerNotFound.setForeground(Color.RED);
		lblServerNotFound.setBounds(267, 164, 86, 14);
		lblServerNotFound.setVisible(false);
		add(lblServerNotFound);
	}
}
